package org.libra {
	import com.ipop.config.Config;
	import flash.text.TextFormat;
	import org.libra.basic.Container;
	import org.libra.events.LibraEvent;
	import org.libra.geom.IntDimension;
	import org.libra.geom.IntPoint;
	import org.libra.utils.JColor;
	
	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public final class JOptionFrame extends JFrame {
		
		public static const OK_STR:String = "确定";
		public static const CANCEL_STR:String = "取消";
		
		public static const BTN_SIZE:IntDimension = new IntDimension(50, 20);
		public static const FRAME_SIZE:IntDimension = new IntDimension(246, 156);
		public static const MSG_SIZE:IntDimension = new IntDimension(246, 136);
		
		public static const OK:int = 1; //00001
		public static const CANCEL:int = 2;//00010
		
		private static var win:JWindow;
		private static var instance:JOptionFrame;
		
		private var okButton:JButton;
		private var cancelButton:JButton;
		private var msgLb:JLabel;
		private var defaultTf:TextFormat;
		
		public var handler:Function;
		
		public function JOptionFrame(owner:Container, modal:Boolean = true, titleBarHeight:int = 20, titleDragable:Boolean = true) {
			super(owner, modal, titleBarHeight, titleDragable);
			defaultTf = new TextFormat();
			defaultTf.leading = 5;
		}
		
		private function getMsgLb(msg:String, html:Boolean):JLabel { 
			if (!this.msgLb) {
				this.msgLb = new JLabel();
				this.msgLb.setSize(MSG_SIZE);
				this.msgLb.setLocation(new IntPoint(0, 20));
			}
			if (html) msgLb.setHtmlText(msg);
			else msgLb.setText(msg);
			msgLb.getTextField().setTextFormat(defaultTf);
			msgLb.setForeground(JColor.WHITE);
			return msgLb;
		}
		
		private function getOKButton():JButton {
			if (this.okButton) {
				return okButton;
			}
			okButton = new JButton(JOptionFrame.OK_STR);
			okButton.setSize(BTN_SIZE);
			return okButton;
		}
		
		private function getCancelButton():JButton {
			if (this.cancelButton) return cancelButton;
			cancelButton = new JButton(JOptionFrame.CANCEL_STR);
			cancelButton.setSize(BTN_SIZE);
			return cancelButton;
		}
		
		public static function setWin(win:JWindow):void {
			JOptionFrame.win = win;
		}
		
		override public function tryToClose():void {
			super.tryToClose();
			this.getOKButton().removeActionListener(onOKBtnHandler);
			this.getCancelButton().removeActionListener(onCancelBtnHandler);
		}
		
		public static function showMessageDialog(msg:String, finishHandler:Function = null, buttons:int = OK, html:Boolean = false):void {
			var frame:JOptionFrame = getInstance();
			frame.handler = finishHandler;
			frame.append(frame.getMsgLb(msg, html));
			var btnCounter:int = 0;
			var btn:JButton;
			if((buttons & OK) == OK){
				btn = frame.getOKButton();
				frame.append(btn);
				btn.addActionListener(onOKBtnHandler);
				frame.setDefaultButton(btn);
				btnCounter += 1;
			}
			if ((buttons & CANCEL) == CANCEL) {
				btn = frame.getCancelButton();
				frame.append(btn);
				btn.addActionListener(onCancelBtnHandler);
				btnCounter += 1;
			}
			if (btnCounter == 1) {
				//frame.getOKButton().setLocation(new IntPoint((FRAME_SIZE.getWidth() - BTN_SIZE.getWidth()) >> 1, FRAME_SIZE.getHeight() - 30));
				frame.getOKButton().setLocation(new IntPoint(120, 100));
			}else {
				//frame.getOKButton().setLocation(new IntPoint(((FRAME_SIZE.getWidth() >> 1) - BTN_SIZE.getWidth()) >> 1, FRAME_SIZE.getHeight() - 30));
				//frame.getCancelButton().setLocation(new IntPoint((FRAME_SIZE.getWidth() >> 1) + ((FRAME_SIZE.getWidth() - BTN_SIZE.getWidth()) >> 1), FRAME_SIZE.getHeight() - 30));
			}
			frame.show();
		}
		
		static private function onCancelBtnHandler(evt:LibraEvent):void {
			var handler:Function = getInstance().handler;
			if (handler != null) {
				handler(JOptionFrame.CANCEL);
			}
			getInstance().tryToClose();
		}
		
		static private function onOKBtnHandler(evt:LibraEvent):void {
			var handler:Function = getInstance().handler;
			if (handler != null) {
				handler(JOptionFrame.OK);
			}
			getInstance().tryToClose();
		}
		
		private static function getInstance():JOptionFrame {
			if (instance) return instance;
			if (win) {
				instance = new JOptionFrame(win);
				instance.setCloseable(false);
				instance.setSize(FRAME_SIZE);
				var location:IntPoint = new IntPoint((Config.STAGE_CONFIG.stage.width - instance.getWidth()) >> 1,
													 (Config.STAGE_CONFIG.stage.height - instance.getHeight()) >> 1);
				instance.setLocation(location);
				return instance;
			}
			throw new Error("the win can't be NULL!");
		}
		
	}

}